package com.bsoft.gol.hcs.clinical.dao;

import com.bsoft.gol.hcs.clinical.bean.req.OutpatientDocRequest;
import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.annotation.RpcService;
import ctd.util.converter.ConversionUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import pcn.cloudClinic.OdsVisitMedical;
import com.bsoft.gol.hcs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class OdsVisitMedicalDAO extends HibernateSupportDelegateDAO<OdsVisitMedical> {

    public OdsVisitMedicalDAO() {
        this.setKeyField("id");
    }
    @RpcService
    @DAOMethod(sql = "select count(1) from OdsVisitMedical where consultId =:consultId")
    public abstract Long getOdsVisitMedicalCountByConsultId(@DAOParam("consultId") String consultId);
    @RpcService
    @DAOMethod(sql = "from OdsVisitMedical where consultId =:consultId")
    public abstract OdsVisitMedical getOdsVisitMedicalByConsultId(@DAOParam("consultId") String consultId);
    @RpcService
    public  List<OdsVisitMedical> queryOdsVisitMedical(final OdsVisitMedical odsVisitMedical){
        HibernateStatelessResultAction<List<OdsVisitMedical>> action = new AbstractHibernateStatelessResultAction<List<OdsVisitMedical>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("select id, hospital_code, emergency_number, patient_code, main_complaint, medical_history, " +
                        "  past_history, supplementary_examination, physical_examination, handling_opinions" +
                        " ,consult_type , consult_id, create_time ,visit_no from ods_visit_medical where 1=1 ");
                if (StringUtils.isNotEmpty(odsVisitMedical.getHospitalCode())) {
                    stringBuffer.append(" and hospital_code = '" + odsVisitMedical.getHospitalCode()+"'");
                }
                if (StringUtils.isNotEmpty(odsVisitMedical.getConsultId())) {
                    stringBuffer.append(" and consult_id = '" + odsVisitMedical.getConsultId()+"'");
                }
                if (StringUtils.isNotEmpty(odsVisitMedical.getConsultType())) {
                    stringBuffer.append(" and consult_type = '" + odsVisitMedical.getConsultType()+"'");
                }
                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                sqlQuery.addEntity(OdsVisitMedical.class);
                setResult(sqlQuery.list());
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    @RpcService
    public  List<OdsVisitMedical> queryOdsVisitMedicalList(final OutpatientDocRequest req){
        HibernateStatelessResultAction<List<OdsVisitMedical>> action = new AbstractHibernateStatelessResultAction<List<OdsVisitMedical>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("select id, hospital_code as hospitalCode, emergency_number as emergencyNumber, patient_code AS patientCode, main_complaint as  mainComplaint, medical_history AS medicalHistory, " +
                        "  past_history AS pastHistory, supplementary_examination AS supplementaryExamination, physical_examination AS physicalExamination, handling_opinions AS handlingOpinions" +
                        " ,consult_type AS consultType, consult_id AS consultId, create_time AS createTime from ods_visit_medical where 1=1 ");
                if (CommonUtils.isNotEmpty(req.getConsultList())){
                    stringBuffer.append(" and consult_id in (:consultList)");
                }
                stringBuffer.append("order by createTime desc");

                SQLQuery sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                if (CommonUtils.isNotEmpty(req.getConsultList())){
                    sqlQuery.setParameterList("consultList",req.getConsultList());
                }
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
                List<OdsVisitMedical>  odsVisitMedicals=new ArrayList<>();
                List<Map> list = sqlQuery.list();
                if(CommonUtils.isNotEmpty(list)){
                    for (Map map : list) {
                        odsVisitMedicals.add(ConversionUtils.convert(map, OdsVisitMedical.class));
                    }

                }
                setResult(odsVisitMedicals);
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

}
